﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
using umbraco.editorControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using umbraco.NodeFactory;
using System.Web.SessionState;
using umbraco.BusinessLogic.Actions;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic.property;
using umbraco.interfaces;
using ExtendedWorkFlow.Helpers;
using ExtendedWorkFlow.Commands;

namespace ExtendedWorkFlow.Commands
{

    public class WorkFlowCommandBinding
    {

        #region Variables

        IExtendedWorkFlowCommand _CommandInstance;
        Node _Command;
        WorkFlow _WorkFlow;

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor to create ExtendedWorkFlow command object
        /// </summary>
        /// <param name="CommandInstance">Instance object of the IExtendedWorkFlowCommand</param>        
        /// <param name="Command">Command node being executed</param>
        /// <param name="ExtendedWorkFlow">ExtendedWorkFlow containing document current state</param>
        public WorkFlowCommandBinding(IExtendedWorkFlowCommand CommandInstance, Node Command, WorkFlow WorkFlow)
        {
            this._CommandInstance = CommandInstance;            
            this._Command = Command;
            this._WorkFlow = WorkFlow;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Command Instance of IExtendedWorkFlowCommand interface
        /// </summary>
        public IExtendedWorkFlowCommand CommandInstance { get { return this._CommandInstance; } }

        /// <summary>
        /// Command node
        /// </summary>
        public Node Command { get { return this._Command; } }

        /// <summary>
        /// ExtendedWorkFlow associated with this command
        /// </summary>
        public WorkFlow WorkFlow { get { return this._WorkFlow; } }

        #endregion

        #region Public Functions

        /// <summary>
        /// DoAction performs action on the command object if the Document, CommandObject and ExtendedWorkFlowCommand exists
        /// </summary>
        /// <returns>Bool indicating if successful</returns>
        public bool DoAction()
        {
            if (this._CommandInstance != null &&
                this._Command != null &&
                this._WorkFlow != null)
            {
                string strParameters =
                    (this._Command.Properties["parameters"] != null ? this._Command.Properties["parameters"].Value : string.Empty);

                System.Diagnostics.Debug.WriteLine("Starting ExtendedWorkFlow command [" + this.Command.Name + "]");

                bool blnResult = this._CommandInstance.RunCommand(
                    strParameters,
                    this._Command,
                    this._WorkFlow);

                System.Diagnostics.Debug.WriteLine("Finished ExtendedWorkFlow command [" + this.Command.Name + "]");

                return blnResult;
            }

            return false;
        }

        #endregion

    }

}
